package com.zhuiyi.springbootmybatis.controller;


import com.zhuiyi.springbootmybatis.entity.User;
import com.zhuiyi.springbootmybatis.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/getall")
    public List<User> getAllUsers() {
        return userService.getUsers();
    }

    @RequestMapping("/adduser")
    public String addUser() {
        User user=new User();
        user.setUsername("www");
        user.setPassword("31313");
        userService.addUser(user);
        return " add user success";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id) {
        userService.deleteUser(id);
        return "delete user success";
    }
    @RequestMapping("/update/{id}")
    public String update(@PathVariable int id) {
        userService.updateUser(new User(id,"王二","1591"));
        return "update user success";
    }

    @RequestMapping("/getone/{id}")
    public User getOneUser(@PathVariable int id){
        User user = userService.getUser(id);
        return user;
    }


    /**
     * 登录接口
     * @param
     * @return
     * 当前端请求传Json对象则后端使用@RequestParam注解；
     * 当前端请求传Json对象的字符串则后端使用@RequestBody注解。
     */
    @PostMapping("/login" )
    public String login(@RequestParam("username") String uname,@RequestParam("password") String pwd){
        String username = uname;
        String password = pwd;
        User user =new User(username,password);
        String loginRes = userService.login(user);
        if (loginRes.equals("success")) {
            return "success";
        } else {
            return "fail";
        }

    }


}
